﻿using DevExpress.Web;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using 聚欣订单分析.PublicClass;

namespace 聚欣订单分析.OrderAna
{
    public partial class DataEnd : System.Web.UI.Page
    {
        public bool IsMobile = false;
        string strSql = "";
        PLSQLClass psql = new PLSQLClass();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserName"] == null)
            {
                Response.Redirect("../login.aspx");
            }

            if (Session["qy"] != null)
            {
                if (Session["qy"].ToString() != "")
                {
                    if (Session["qx"].ToString() == "1")
                    {
                        strSql = "SELECT * FROM [v_maintable] where stateValue='订单结束' and addperson='" + Session["realname"].ToString() + "' ORDER BY action2 DESC,addtime DESC";
                    }
                    else
                    {
                        strSql = "SELECT * FROM [v_maintable] where stateValue='订单结束' and region in('" + Session["Qy"].ToString().Replace(",", "','") + "') ORDER BY action2 DESC,addtime DESC";
                    }

                    MobileLoad();
                    if (ASPxGridView1.IsCallback)
                    {
                        if (hfCxsql.Value != "")
                        {
                            this.SqlDataSource1.SelectCommand = hfCxsql.Value;
                        }
                        else
                        {
                            this.SqlDataSource1.SelectCommand = strSql;
                        }
                        ASPxGridView1.DataBind();
                    }
                    else
                    {
                        this.SqlDataSource1.SelectCommand = strSql;
                    }
                }
                else
                {
                    PublicClass.JsHelper.AlertAndRedirect("权限不足", "../index.aspx");
                }
            }
            else
            {
                PublicClass.JsHelper.AlertAndRedirect("权限不足", "../index.aspx");
            }
        }

        //获取跟踪情况
        public string Gznew(object id)
        {
            DataTable dt = psql.SqlBind("SELECT top 1 * from detailstable where projectid=" + id.ToString() + " ORDER BY commenttime DESC,[situationtime] DESC ");
            if (dt.Rows.Count > 0)
            {
                string strC = "跟踪情况：" + dt.Rows[0]["situation"].ToString();
                if (dt.Rows[0]["comment"].ToString() != "")
                {
                    strC += "<br/>批注：" + dt.Rows[0]["comment"].ToString();
                }
                return strC;
            }
            return "";
        }

        // 搜索
        protected void btnSel_Click(object sender, EventArgs e)
        {
            string strContent = txtContent.Value.Trim();

            if (strContent != "")
            {
                hfCxsql.Value = "SELECT * FROM [v_maintable] where stateValue='订单结束' and region in('" + Session["Qy"].ToString().Replace(",", "','") + "') and addperson='" + strContent + "' or name like '%" + strContent + "%' or stateValue like '%" + strContent + "%' or region = '" + strContent + "' or projectValue = '" + strContent + "' or productValue ='" + strContent + "' ORDER BY action2 DESC,addtime DESC";
            }
            else
            {
                hfCxsql.Value = strSql;
            }
            this.SqlDataSource1.SelectCommand = hfCxsql.Value;
            ASPxGridView1.DataBind();
        }

        //验证
        protected void ASPxGridView1_RowValidating(object sender, DevExpress.Web.Data.ASPxDataValidationEventArgs e)
        {
            if (e.NewValues["name"] == null)
            {
                e.RowError = "项目名称不能为空！";
                return;
            }
            if (e.NewValues["region"] == null)
            {
                e.RowError = "区域不能为空！";
                return;
            }
            if (e.NewValues["money"] == null)
            {
                e.RowError = "金额不能为空！";
                return;
            }
            if (e.NewValues["projecttype"] == null)
            {
                e.RowError = "项目类型不能为空！";
                return;
            }
            if (e.NewValues["producttype"] == null)
            {
                e.RowError = "产品类型不能为空！";
                return;
            }
            if (e.NewValues["state"] == null)
            {
                e.RowError = "状态不能为空！";
                return;
            }
        }

        protected void ASPxGridView1_CustomButtonCallback(object sender, DevExpress.Web.ASPxGridViewCustomButtonCallbackEventArgs e)
        {
            string strID = this.ASPxGridView1.GetRowValues(e.VisibleIndex, "ID").ToString();
            string mname = this.ASPxGridView1.GetRowValues(e.VisibleIndex, "name").ToString();
            string addperson = this.ASPxGridView1.GetRowValues(e.VisibleIndex, "addperson").ToString();
            if (e.ButtonID == "CbtnGz")
            {
                ASPxGridView view = sender as ASPxGridView;
                view.JSProperties["cpid"] = strID;
                view.JSProperties["cpmname"] = mname;
                view.JSProperties["cpaddperson"] = addperson;
            }
        }

        private void MobileLoad()
        {
            if (PublicClass.PublicClass.IsMobileLogin())
            {
                IsMobile = true;
                ASPxGridView1.Columns["name"].Visible = false;
                ASPxGridView1.Columns["addperson"].Visible = false;
                ASPxGridView1.Columns["addtime"].Visible = false;
                ASPxGridView1.Columns["description"].Visible = false;
                ASPxGridView1.Columns["projecttype"].Visible = false;
                ASPxGridView1.Columns["producttype"].Visible = false;

                ASPxGridView1.SettingsPager.PageSize = 10;
                ASPxGridView1.Settings.ShowPreview = true;
                ASPxGridView1.Settings.ShowHorizontalScrollBar = true;
            }
        }
    }
}